package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Car;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @CopyRight 恒信国通信息科技
 * @Author wulei
 * @Date 2018/8/27
 */

public class CarListAdapter extends BaseAdapter {
    private List<Car> carList;
    private Context context;

    public CarListAdapter(List<Car> carList, Context context) {
        this.carList = carList;
        this.context = context;
    }

    public void setCarList(List<Car> carList) {
        this.carList = carList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return carList.size();
    }

    @Override
    public Object getItem(int position) {
        return carList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_car, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        Car car = carList.get(position);
        holder.carNum.setText(car.getPlateNo());
        holder.carTime.setText(car.getPassTime());
        if(car.getDirection() == 0){
            holder.carType.setText(R.string.enter);
            holder.carType.setEnabled(true);
        }else if(car.getDirection() == 1){
            holder.carType.setText(R.string.exit);
            holder.carType.setEnabled(false);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.car_num)
        HXTextView carNum;
        @BindView(R.id.car_time)
        HXTextView carTime;
        @BindView(R.id.car_type)
        HXTextView carType;

        public ViewHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }
    }
}
